| Conditions | 1 |
| Paths | 1 |
| Total Lines | 88 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | var jsrsasign = require('jsrsasign'); |
||
| 92 | describe('ChainPathBuilder', function() { |
||
| 93 | var fixture = certfile.test_cert; |
||
| 94 | var entityCert = certFromEncoding(fixture.entityCertificate, "base64"); |
||
| 95 | var rootCert = certFromEncoding(fixture.rootCertificate, "base64"); |
||
| 96 | var intermediates = fixture.intermediates.map(function(base64) { |
||
| 97 | return certFromEncoding(base64, "base64"); |
||
| 98 | }); |
||
| 99 | var chainValidTime = fixture.chainValidTime; |
||
| 100 | |||
| 101 | it('builds a valid certificate chain', function(cb) { |
||
| 102 | var numCerts = 2 + intermediates.length; |
||
| 103 | var builder = new ChainPathBuilder([rootCert]); |
||
| 104 | var path = builder.shortestPathToTarget(entityCert, intermediates); |
||
| 105 | |||
| 106 | assert.equal(path.length, numCerts, "expecting " + numCerts + " certificates in total for path"); |
||
| 107 | |||
| 108 | var validator = new ChainPathValidator({ |
||
| 109 | currentTime: chainValidTime |
||
| 110 | }, path); |
||
| 111 | |||
| 112 | assert.doesNotThrow(function() { |
||
| 113 | validator.validate(); |
||
| 114 | }, 'no errors expected during validation'); |
||
| 115 | |||
| 116 | cb(); |
||
| 117 | }); |
||
| 118 | |||
| 119 | function testCertificateValidity(now) { |
||
| 120 | var builder = new ChainPathBuilder([rootCert]); |
||
| 121 | var path = builder.shortestPathToTarget(entityCert, intermediates); |
||
| 122 | assert.equal(path.length, 3, "expecting 3 certificates in total for path"); |
||
| 123 | |||
| 124 | var validator = new ChainPathValidator({ |
||
| 125 | currentTime: now |
||
| 126 | }, path); |
||
| 127 | |||
| 128 | var err; |
||
| 129 | try { |
||
| 130 | validator.validate(); |
||
| 131 | } catch (e) { |
||
| 132 | err = e; |
||
| 133 | } |
||
| 134 | |||
| 135 | assert.ok(typeof err === "object"); |
||
| 136 | assert.equal(err.message, "Certificate is not valid"); |
||
| 137 | } |
||
| 138 | |||
| 139 | [rootCert].concat(intermediates).concat(entityCert).map(function(cert, i) { |
||
| 140 | it('rejects if `now` is after #' + i + ' certs `notAfter`', function(cb) { |
||
| 141 | // this timestamp conflicts with the root certificates notBefore |
||
| 142 | var now = jsrsasign.zulutodate(cert.getNotAfter()); |
||
| 143 | now = addDays(now, 2); |
||
| 144 | testCertificateValidity(now); |
||
| 145 | cb(); |
||
| 146 | }); |
||
| 147 | |||
| 148 | it('rejects if `now` is before #' + i + ' certs `notBefore`', function(cb) { |
||
| 149 | // this timestamp conflicts with the root certificates notBefore |
||
| 150 | var now = jsrsasign.zulutodate(rootCert.getNotBefore()); |
||
| 151 | now = subDays(now, 2); |
||
| 152 | testCertificateValidity(now); |
||
| 153 | cb(); |
||
| 154 | }); |
||
| 155 | }); |
||
| 156 | |||
| 157 | function testNoCertificationPath(trusted, intermediate, end) { |
||
| 158 | var err; |
||
| 159 | try { |
||
| 160 | var builder = new ChainPathBuilder(trusted); |
||
| 161 | builder.shortestPathToTarget(end, intermediate); |
||
| 162 | } catch (e) { |
||
| 163 | err = e; |
||
| 164 | } |
||
| 165 | |||
| 166 | assert.equal(typeof err, "object"); |
||
| 167 | assert.equal(err.message, "No certificate paths found"); |
||
| 168 | } |
||
| 169 | |||
| 170 | it('errors if intermediate certificate is missing', function(cb) { |
||
| 171 | testNoCertificationPath([rootCert], [], entityCert); |
||
| 172 | cb(); |
||
| 173 | }); |
||
| 174 | |||
| 175 | it('errors if root certificate is missing', function(cb) { |
||
| 176 | testNoCertificationPath([], intermediates, entityCert); |
||
| 177 | cb(); |
||
| 178 | }); |
||
| 179 | }); |
||
| 180 | |||
| 242 |